import "babel-polyfill"
import Vue from 'vue'
import VueRouter from 'vue-router'
import router from './router'
import index from './app.vue'
import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/dist/css/swiper.css'
import "element-ui/lib/theme-chalk/index.css";
import myMixin from './myMixin' //全局混入
import webHeader from './page/components/header'

import {
  Menu,
  Submenu,
  Dialog,
  MenuItem,
  MenuItemGroup,
  Carousel,
  CarouselItem,
  Loading,
  Drawer,
  Input,
  Form,
  FormItem,
  InputNumber,
  Button,
  ButtonGroup,
  Popover,
  Row,
  Col,
  MessageBox,
  RadioGroup,
  RadioButton,
  Select,
  Option,
  Upload,
  Image,
  Pagination,
  Dropdown,
  DropdownMenu,
  DropdownItem,
  Checkbox,
} from 'element-ui';
import lang from 'element-ui/lib/locale/lang/en'
import locale from 'element-ui/lib/locale'
locale.use(lang)
Vue.use(Menu);
Vue.use(Submenu);
Vue.use(MenuItem);
Vue.use(MenuItemGroup);
Vue.use(Carousel);
Vue.use(CarouselItem);
Vue.use(Drawer);
Vue.use(Input);
Vue.use(Dialog);
Vue.use(Button);
Vue.use(ButtonGroup);
Vue.use(InputNumber);
Vue.use(Popover);
Vue.use(Form);
Vue.use(FormItem);
Vue.use(Row);
Vue.use(Col);
Vue.use(RadioGroup);
Vue.use(RadioButton);
Vue.use(Select);
Vue.use(Option);
Vue.use(Upload);
Vue.use(Image);
Vue.use(Pagination);
Vue.use(Dropdown);
Vue.use(DropdownMenu);
Vue.use(DropdownItem);
Vue.use(Checkbox);
Vue.use(Loading.directive);
Vue.prototype.$loading = Loading.service;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.use(VueAwesomeSwiper)
Vue.use(VueRouter)
Vue.mixin(myMixin)
Vue.component('web-header', webHeader)
import i18n from './language/i18n'
router.beforeEach((to, from, next) => {
  if (to.path) {
    if (window._hmt) {
      window._hmt.push(['_trackPageview', '/#' + to.fullPath])
    }
  }
  const token = JSON.parse(localStorage.getItem('userInfo')) ? JSON.parse(localStorage.getItem('userInfo')).token : ''
  const whiteList = ['/registered', '/forgotPassword', '/', '/login', '/productDetail', '/customize'] // 免登录
  if (token) {
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next();
    } else {
      next({ path: '/login' });
      window.location.reload()
    }
  }
});
new Vue({
  el: '#index',
  router,
  i18n,
  render: v => v(index),
})
